const DrawPolygon = require('../models').DrawPolygon;


module.exports = {
    async listAll(req, res) {
        let para = {
            order: ['id'],
            limit: req.query.pageSize || 15,
            offset: req.query.curPage * req.query.pageSize || 0,
            order: [['id', 'desc']],            
        }
        if(req?.query?.name?.length>0){
            para.where = {
                name: {
                    [Op.like]: '%' + req.query.name + '%'
                }
            }
        }
        return await DrawPolygon.findAndCountAll(para).then(drawPolygon => res.json(drawPolygon))
    },    

    async create(req, res) {         
         

          const geojson = 
          {
            type: "Polygon",
            coordinates: [req.body.geo[0].map(coord => [coord.lng, coord.lat])]
          }
          console.log(req.body.geo)
          console.log(geojson)
        return DrawPolygon
            .create({
                name: req.body.name,
                geo: geojson,                
                remark: req.body.remark,
            })
            .then((drawPolygon) => res.status(201).send({
                success: true,
                id: drawPolygon.id,
                message: 'add successful'
            }))
            .catch((error) => res.status(400).send(error));
    },

    async update(req, res) { 
        const geojson = 
          {
            type: "Polygon",
            coordinates: [req.body.geo[0].map(coord => [coord.lng, coord.lat])]
          }
        return await DrawPolygon
            .findByPk(req.params.id)
            .then(drawPolygon => {
                if (!drawPolygon) {
                    return res.status(200).send({
                        success: false,
                        message: 'drawPolygon Not Found',
                    });
                }
                return drawPolygon
                    .update({
                        name: req.body.name || drawPolygon.name,
                        geo: geojson || drawPolygon.geo,
                        remark: req.body.remark || drawPolygon.remark                
                    })
                    .then(() => res.status(200).send(drawPolygon))
                    .catch((error) => res.status(400).send(error));
            })
            .catch((error) => res.status(400).send(error));
    },

    async delete(req, res) {
        return await DrawPolygon
            .findByPk(req.params.id)
            .then(drawPolygon => {
                if (!drawPolygon) {
                    return res.status(200).send({
                        success: false,
                        message: 'drawPolygon Not Found',
                    });
                } else {
                    return drawPolygon
                        .destroy()
                        .then(() => res.status(200).send({
                            success: true,
                            message: 'delete successful'
                        }))
                        .catch((error) => res.status(400).send(error));
                }
            })
            .catch((error) => res.status(400).send(error));
    },
}